﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.Business;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common;
using VipSys.Business;
namespace VipSys.website
{
    public partial class SubExamPlace : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDll();
            }
        }
        private void BindDll()
        {
            ExamPlace query = new ExamPlace();
            query.PlaceType = SystemData.PlaceType.PROVINCE;
            List<ExamPlace> lstExamPlace = new List<ExamPlace>();
            short shRet = ExamPlaceAccess.Instance.GetExamPlaceList(query, ref lstExamPlace);
            if (shRet != SystemData.ReturnValue.OK)
                return;
            this.ddlProvince.Items.Clear();
            foreach (var item in lstExamPlace)
            {
                this.ddlProvince.Items.Add(new ListItem(item.PlaceName, item.ID.ToString()));
            }
            this.ddlProvince.Items.Insert(0, new ListItem("请选择", ""));
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ddlProvince.SelectedValue.ToString()))
                return;
            ExamPlace query = new ExamPlace();
            this.ddlCity.Items.Clear();
            query.ParentID = this.ddlProvince.SelectedValue.ToString();
            List<ExamPlace> lstExamPlace = new List<ExamPlace>();
            short shRet = ExamPlaceAccess.Instance.GetExamPlaceList(query, ref lstExamPlace);
            if (shRet != SystemData.ReturnValue.OK)
                return;
            foreach (var item in lstExamPlace)
            {
                this.ddlCity.Items.Add(new ListItem(item.PlaceName, item.ID.ToString()));
            }
            this.ddlCity.Items.Insert(0, new ListItem("请选择", ""));
        }
        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ddlCity.SelectedValue.ToString()))
                return;
            ExamPlace query = new ExamPlace();
            this.ddlSchool.Items.Clear();
            query.ParentID = this.ddlCity.SelectedValue.ToString();
            List<ExamPlace> lstExamPlace = new List<ExamPlace>();
            short shRet = ExamPlaceAccess.Instance.GetExamPlaceList(query, ref lstExamPlace);
            if (shRet != SystemData.ReturnValue.OK)
                return;
            foreach (var item in lstExamPlace)
            {
                this.ddlSchool.Items.Add(new ListItem(item.PlaceName, item.PlaceName));
            }
            this.ddlSchool.Items.Insert(0, new ListItem("请选择", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.ID = SystemParam.Instance.SysAccount.ID;
            UserAccess.Instance.GetUser(user, ref user);
            if (this.chkOther.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSchool.Text.Trim()))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('未输入考点，不能提交!')</script>");
                    return;
                }
                if (string.IsNullOrEmpty(this.ddlCity.SelectedValue))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('未选择城市，不能提交!')</script>");
                    return; 
                }
                SubExamPlaceBLL.Instance.SaveExamPlace(this.ddlCity.SelectedValue, this.txtSchool.Text.Trim());
                user.ExamPlace = this.txtSchool.Text.Trim();
               
            }
            else
            {
                if (String.IsNullOrEmpty(this.ddlSchool.SelectedValue))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('未选择考点，不能提交!')</script>");
                    return;
                }
                user.ExamPlace = this.ddlSchool.SelectedValue;
            }
            short shRet = UserAccess.Instance.Edit(user);
            if (shRet != SystemData.ReturnValue.OK)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交失败')</script>");
                return;
            }
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交成功，请等待机构为您分房！')</script>");
            Response.Redirect("UserInfo.aspx");
        }

        protected void chkOther_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOther.Checked)
            {
                this.txtSchool.Visible = true;
                this.ddlSchool.Visible=false;
            }
            else {
                this.ddlSchool.Visible = true;
                this.txtSchool.Visible = false;
            }
        }
       
    }
}